# /usr/bin/python
# -*- coding: utf-8 -*-

import os
os.path.append('/sbin')
import sys
import re
import time
import json
import commands
import logging

###
LOGDIR="/data/logs/nginx/access/"

def get_stream_logger(name):
    """获取一个输出到标准输出的 logger"""
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)

    fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    formatter = logging.Formatter(fmt)
    
    record = logging.FileHandler("/data/logs/nginx/nginx_cron.log")
    record.setFormatter(formatter)
    record.setLevel(logging.INFO)
    logger.addHandler(record)

    return logger

def get_file(logdir):
    file_name = []
    files = os.walk(logdir)

    for file in list(files)[0][2]:
        if re.match(r".+?.log$",file):
            file_name.append(LOGDIR + file)

    return file_name

def get_file_end(logfile):
    f = file(logfile,'r')
    f.seek(0,2)
    file_end = f.tell()
    f.close()
    return file_end

def status_count(logfile,start):
    domain = '.'.join(logfile.split('.')[1:-1])
    statistic = {
        domain:{
            "2XX": 0,
            "3XX": 0,
            "4XX": 0,
            "5XX": 0
        }
    }

    filter = r"\"(?P<datetime>.+?)\" \| (?P<domain>.+?) \| (?P<status>.+?) \| (?P<requests_time>.+?) \| (?P<thttp_x_forwarded_for>.+?) \| (?P<body_bytes_sent>.+?) \| (?P<requests>.*)"
    f = file(logfile,'r')
    f.seek(start,0)
    lines = f.readlines()

    for line in lines:
        res = re.match(filter,line).groupdict()
        statistic[domain][res["status"][0]+"XX"] += 1

    f.close()

    return statistic

def get_payload(data):
    payload = []
    for domain in data:
        for item in data[domain]:
            payload.append(get_template(domain,item,data[domain][item],ip))
    
    return payload

def get_template(endpoint,metirc,value,ip):
    template = {
        "endpoint": str(ip),
        "metric": str(metirc),
        "timestamp": int(time.time()),
        "step": 60,
        "value": value,
        "counterType": "GAUGE",
        "tags": "domain=" + str(endpoint),
    }
    return template

def read_logfile(logfile):
    print "read logfile:%s"%logfile
    report = status_count(logfile,end_point[logfile])
    payload = get_payload(report)
    print payload
    logger.info(payload)
    end_point[logfile] = get_file_end(logfile)
    try:
        import requests
        res = requests.post("http://127.0.0.1:1988/v1/push",data=json.dumps(payload))
    except ImportError:
        print "Please install the models Requests using: \"pip install requests\""
        sys.exit(-1)

    print res
    logger.info(res)


if __name__ == "__main__":
    files = get_file(LOGDIR)
    global end_point
    ip_cmd = "/usr/sbin/ifconfig | grep inet | grep -v inet6 | grep -v '127.0.0.1' | grep '10.205' | awk '{print $2}' | awk -F ':' '{print $2}'"
    ip = commands.getoutput(ip_cmd)

    logger = get_stream_logger("nginx_cron")

    try:
        with open('/tmp/nginx_endpoint.log','r') as f:
            end_point = json.loads(f.read())
    except Exception:
        end_point = {}
        for file_name in files:
            end_point[file_name] = get_file_end(file_name)

    for file_name in files:
        if end_point.has_key(file_name):
            if end_point[file_name] > get_file_end(file_name):
                end_point[file_name] = 0
            try:
                read_logfile(file_name)
            except Exception:
                continue
        else:
            end_point[file_name] = 0
            try:
                read_logfile(file_name)
            except Exception:
                continue

    with open('/tmp/nginx_endpoint.log','w') as f:
        f.write(json.dumps(end_point))
